package com.hust.service.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.hust.domain.ApportionResult;
import com.hust.service.IApportionService;
import com.hust.utils.Utils;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Set;

@Service
public class ApportionServiceImpl implements IApportionService {
    @Override
    public ApportionResult calculate(Date date) {
        String resultFile = Utils.fullPath("models\\apportion\\source_analysis_result.xlsx");
        File file = new File(resultFile);
        if (file.exists()) file.delete();

        String modelFile = Utils.fullPath("models\\apportion\\apportion.py");
        try {
            ProcessBuilder pb = new ProcessBuilder("python",modelFile);
            Process process = pb.start();
            //等待执行完成的后退出状态码，0表示成功，其他是失败
            process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (file.exists()) {
            ApportionResult result = new ApportionResult();
            result.FromXlsx(resultFile);
            return result;
        } else return null;
    }
}
